<?php
/**
 * 后台首页
 *
 * Author:李强
 * Company:上海煦鸣网络科技有限公司
 * Date:2018-01-03
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\CommonController;
use App\Models\Order;
use App\Models\Permission;
use App\Models\Shop;
use App\Models\Goods;
use App\Models\User;
use App\Services\Service\Helper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class IndexController extends CommonController
{
    protected $view_prefix = 'admin.';

    /**
     * 后台管理首页
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $this->assign('user', Auth::user());
        //获取顶部菜单
        $top_menus = $menus = Permission::topmenu();
        $this->assign('top_menus', $top_menus);

        //获取左边菜单
        $left_menus = [];
        foreach ($top_menus as $k => $top) {
            $menus = Permission::submenus($top['id']);
            if (false !== $menus) {
                $left_menus[$top['id']] = $menus;
            }
        }
        $this->assign('left_menus', $left_menus);
        return $this->display('index.index');
    }

    /**
     * 后台默认面板页面
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function dashboard(Request $request)
    {
        return $this->display('index.dashboard');
    }

    /**
     * 获取顶部菜单
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function topmenu(Request $request)
    {
        if ($request->ajax()) {
            $menus = Permission::topmenu();
            return $this->success('success', $menus);
        }
    }

    /**
     * 根据顶部菜单初始化左边菜单
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function leftmenu(Request $request)
    {
        if ($request->ajax()) {
            try {
                $pid = $request->input('pid', 0);
                $menus = Permission::submenus($pid);
            } catch (\Exception $e) {
                dd($e->getMessage());
            }
            return $this->success('success', $menus);
        }
    }

    /**
     * 管理界面解锁
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function unlock(Request $request)
    {
        if ($request->ajax()) {
            $password = $request->input('password', '');
            if ($password == '') {
                return $this->error('请输入密码');
            } else {
                if (Hash::check($password, Auth::user()->password)) {
                    return $this->success('success');
                } else {
                    return $this->error('密码错误');
                }
            }
        }
    }

    /**
     * 设置参数选项
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function setoption(Request $request)
    {
        $type = $request->input('type', 1);
        if ($type == 1) {
            $value = $request->input('value', '');
            $this->assign('value', $value);
            return $this->display('index.setoption');
        } elseif ($type == 2) {//接口方法请求参数
            return $this->display('interface.request_param');
        } elseif ($type == 3) {//接口方法返回参数
            return $this->display('interface.return_param');
        }
    }

    /**
     * 首页初始化汇总项目
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function initdata(Request $request)
    {
        if ($request->ajax()) {
            $datas = [];
            //今日补单
            $today = Helper::toDate(time(),'Y-m-d');
            $start = $today;
            $result = Order::where(['status'=>4])->whereBetween('order_time',[$start." 00:00:00",$start." 23:59:59"])
                ->selectRaw('count(1) as count,sum(commission) comm')->first();
            $datas['today_success_num'] = !is_null($result)?$result->count:0;
            $datas['today_success_comm'] = !is_null($result)?(is_null($result->comm)?0:$result->comm):0;
            //进行中
            $result = Order::where('status','!=',4)->where('status','>',0)->whereBetween('order_time',[$start." 00:00:00",$start." 23:59:59"])
                ->selectRaw('count(1) as count,sum(commission) comm')->first();
            $datas['today_waiting_num'] = !is_null($result)?$result->count:0;
            $datas['today_waiting_comm'] = !is_null($result)?(is_null($result->comm)?0:$result->comm):0;
            //商家总数
            $datas['shop_num'] = Shop::where('status',1)->count();
            //商品总数
            $datas['goods_num'] = Goods::normal()->where('status',1)->count();
            //本周
            $start = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
            $result = Order::where(['status'=>4])->whereBetween('order_time',[$start." 00:00:00",$today." 23:59:59"])
                ->selectRaw('count(1) as count,sum(commission) comm')->first();
            $datas['week_success_num'] = !is_null($result)?$result->count:0;
            $datas['week_success_comm'] = !is_null($result)?(is_null($result->comm)?0:$result->comm):0;
            //本月
            $start = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));
            $result = Order::where(['status'=>4])->whereBetween('order_time',[$start." 00:00:00",$today." 23:59:59"])
                ->selectRaw('count(1) as count,sum(commission) comm')->first();
            $datas['month_success_num'] = !is_null($result)?$result->count:0;
            $datas['month_success_comm'] = !is_null($result)?(is_null($result->comm)?0:$result->comm):0;
            return $this->success('success', $datas);
        }
    }

    /**
     * 首页曲线折图
     *
     * @param Request $request
     * @param int $type 1会员
     * @return \Illuminate\Http\JsonResponse
     */
    public function index_tongji(Request $request)
    {
        $type = $request->input('type', 1);//1-7天2-14天3-30日
        if ($request->ajax()) {
            $date_end = strtotime(date('Y-m-d', time()))-86400;
            //获取近七天会员注册
            if ($type == 1) {
                $date_begin = $date_end - 6 * 86400;
                $title = "近七日 ";
            }elseif($type==2){
                $date_begin = $date_end - 13 * 86400;
                $title = "近两周 ";
            }elseif($type==3){
                $date_begin = $date_end - 29 * 86400;
                $title = "近一个月 ";
            }
            $days = [];
            $datas = [];
            for ($day = $date_begin; $day <= $date_end; $day += 86400) {
                $current_day = Helper::toDate($day, 'Y-m-d');
                $days[] = $current_day;
                //补单数量
                $count = Order::where('status',4)->whereBetween('order_time', [$current_day . " 00:00:00", $current_day . " 23:59:59"])->count();
                $datas[] = $count;
            }
            return $this->success('success', ['dates' => $days, 'datas' => $datas,'title'=>$title]);
        }
    }

    /**
     * 列表选择器
     *
     * @param Request $request
     * @param $name
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function choice(Request $request, $name)
    {
        $type = $request->input('type', 0);//0代表单选1多选
        if ($name == 'shop') {
            //
            $inputs = Helper::getInputs($request, ['opt_type' => '', 'keywords' => '', 'catid' => '', 'page' => 1, 'limit' => 10, 'status' => '']);
            $page = intval($inputs['page']);
            $limit = intval($inputs['limit']);
            $start = ($page - 1) * $limit;

            $where = [];
            $where['normal'][] = [];
            if ($inputs['opt_type'] != '' && $inputs['keywords'] != '') {
                if ($inputs['opt_type'] == 'id') {
                    $where['where'][] = [$inputs['opt_type'] => $inputs['keywords']];
                } else {
                    $where['where'][] = [$inputs['opt_type'], 'like', '%' . $inputs['keywords'] . '%'];
                }
            }
            if ($inputs['status'] != '') {
                $where['where'][] = ['status' => $inputs['status']];
            }
            if ($inputs['catid'] != '') {
                $where['where'][] = ['catid' => $inputs['catid']];
            }
            $result = Shop::queryAll($where, [], [$start, $limit]);
            $result['conditions'] = $inputs;
            $result['choice_type'] = $type;
        }
        return $this->display('index.choice.' . $name, $result);
    }
}
